<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选状态 -->
			<cl-filter label="状态">
				<cl-select prop="status" :options="options.liststatus" />
			</cl-filter>
			<!-- 筛选支付方式 -->
			<cl-filter label="支付方式">
				<cl-select prop="paymentMethod" :options="options.paymentMethod" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索订单号、用户、商品" />
		</cl-row>
		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table"> </cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="order-info" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { reactive } from "vue";

const { service } = useCool();

// 选项
const options = reactive({
	status: [
		{ label: "已取消", value: 0, type: "danger" },
		{ label: "已支付", value: 1, type: "primary" },
		{ label: "已发货", value: 2, type: "warning" },
		{ label: "已取消", value: 3, type: "danger" },
		{ label: "已取消", value: 4, type: "danger" }
	],
	paymentMethod: [
		{ label: "支付宝", value: 0, type: "primary" },
		{ label: "微信", value: 1, type: "success" },
		{ label: "银行卡", value: 2, type: "warning" }
	],
	liststatus: [
		{ label: "待支付", value: 0, type: "danger" },
		{ label: "已支付", value: 1, type: "primary" },
		{ label: "已完成", value: 2, type: "warning" },
		{ label: "已取消", value: 4, type: "danger" }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "订单号",
			prop: "orderNumber",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		// {
		// 	label: "用户",
		// 	prop: "user",
		// 	component: { name: "el-input", props: { clearable: true } },
		// 	required: true
		// },
		{
			label: "商品",
			prop: "product",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "数量",
			prop: "quantity",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "总价",
			prop: "totalPrice",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "状态",
			prop: "status",
			component: {
				name: "el-select",
				options: options.status,
				props: { filterable: true }
			},
			value: [],
			required: true
		},
		{
			label: "支付方式",
			prop: "paymentMethod",
			component: { name: "el-radio-group", options: options.paymentMethod, props: {} },
			value: 0,
			required: true
		},
		// {
		// 	label: "收货地址",
		// 	prop: "deliveryAddress",
		// 	component: { name: "el-input", props: { type: "textarea", rows: 4 } },
		// 	required: true
		// },
		{
			label: "下单时间",
			prop: "orderTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" }
			},
			required: true
		},
		{
			label: "备注",
			prop: "remark",
			component: { name: "el-input", props: { type: "textarea", rows: 4 } }
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "订单号", prop: "orderNumber", minWidth: 140 },
		{ label: "用户", prop: "user", minWidth: 140 },
		{ label: "商品", prop: "product", minWidth: 140 },
		{ label: "数量", prop: "quantity", minWidth: 140 },
		{ label: "总价", prop: "totalPrice", minWidth: 140 },
		{ label: "状态", prop: "status", dict: options.status, minWidth: 120 },
		{
			label: "支付方式",
			prop: "paymentMethod",
			dict: options.paymentMethod,
			minWidth: 120
		},
		{
			label: "收货地址",
			prop: "deliveryAddress",
			showOverflowTooltip: true,
			minWidth: 200
		},
		{
			label: "下单时间",
			prop: "orderTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{ label: "备注", prop: "remark", showOverflowTooltip: true, minWidth: 200 },
		// {
		// 	label: "更新时间",
		// 	prop: "updateTime",
		// 	minWidth: 170,
		// 	sortable: "custom",
		// 	component: { name: "cl-date-text" }
		// },
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.order.info
	},
	(app) => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
